/**
 * <strong>Title : EquipmentStatusController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.pp.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月2日 上午9:54:43<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.condition.EquipmentStatusCondition;
import cn.b.sky.mes.em.service.EquipmentStatusService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.EquipmentStatus;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : EquipmentStatusController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备状态控制器<br> 
 * <strong>Create on : 2015年11月2日 上午9:54:43<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "equipmentStatus")
public class EquipmentStatusController extends SecurityController {
  @Resource
  EquipmentStatusService equipmentStatusService;

  @RequestMapping(value = "equipmentStatus_list")
  public String equipmentStatusList(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentStatusCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<EquipmentStatus> equipmentStatusList = equipmentStatusService.selectEquipmentStatusList(condition);
    int count = equipmentStatusService.selectEquipmentStatusCount(condition);
    condition.setData(equipmentStatusList);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID,  navTabId);
    return "em/equipmentStatus/equipmentStatus_list";
  }

  @RequestMapping(value = "addEquipmentStatus")
  public String addequipmentStatus(String navTabId, Model model) {
    model.addAttribute("pattern", "add");
    model.addAttribute(NAVTABID,  navTabId);
    return "em/equipmentStatus/equipmentStatus_add";
  }

  @RequestMapping(value = "validateEquipmentStatusCode")
  @ResponseBody
  public boolean validateEquipmentStatusCode(String statusCode) {
    if (!StringUtils.isEmpty(statusCode)) {
      EquipmentStatus equipmentStatus = equipmentStatusService.getEquipmentStatus(statusCode, getSiteCode());
      return equipmentStatus == null;
    }
    return true;
  }

  @RequestMapping(value = "checkEquipmentStatusCode")
  @ResponseBody
  public boolean checkEquipmentStatusCode(String statusCode) {
    if (StringUtils.isEmpty(statusCode)) {
      return false;
    } else {
      EquipmentStatus equipmentStatus = equipmentStatusService.getEquipmentStatus(statusCode, getSiteCode());
      return equipmentStatus != null && equipmentStatus.getLockFlag() != 1;
    }
  }

  @RequestMapping(value = "modifyEquipmentStatus")
  public String modifyEquipmentStatus(String navTabId, Model model, String id) {
    model.addAttribute("pattern", "modify");
    EquipmentStatus equipmentStatus = equipmentStatusService.getEquipmentStatus(id, getSiteCode());
    model.addAttribute(NAVTABID,  navTabId);
    model.addAttribute("id", equipmentStatus.getStatusCode());
    model.addAttribute("equipmentStatus", equipmentStatus);
    return "em/equipmentStatus/equipmentStatus_add";
  }

  @RequestMapping(value = "viewEquipmentStatus")
  public String viewEquipmentStatus(String navTabId, Model model, String id) {
    model.addAttribute("pattern", "read");
    EquipmentStatus equipmentStatus = equipmentStatusService.getEquipmentStatus(id, getSiteCode());
    model.addAttribute(NAVTABID,  navTabId);
    model.addAttribute("equipmentStatus", equipmentStatus);
    return "em/equipmentStatus/equipmentStatus_add";
  }

  @RequestMapping(value = "saveEquipmentStatus")
  @ResponseBody
  public DWZResult saveEquipmentStatus(Model model, String navTabId, String id, EquipmentStatus equipmentStatus,
      HttpServletRequest request) {
    if (equipmentStatus == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    setCreateInfo(equipmentStatus);
    equipmentStatus.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      equipmentStatusService.saveEquipmentStatus(equipmentStatus);
      String forwardUrl = "em/equipmentStatus/equipmentStatus_list";
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          navTabId, null, forwardUrl, null);

    } else {
      equipmentStatus.setStatusCode(id);
      equipmentStatusService.updateEquipmentStatus(equipmentStatus);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          navTabId, null, null, null);
    }
  }

  @RequestMapping(value = "deleteEquipmentStatus")
  @ResponseBody
  public DWZResult deleteEquipmentStatus(Model model, String navTabId, EquipmentStatus equipmentStatus,
      HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] equipmentStatuses = ids.split(",");
    int len = equipmentStatuses.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    int failureCount = 0;
    for (int i = 0; i < equipmentStatuses.length; i++) {
      reports[i] = equipmentStatusService.deleteEquipmentStatus(equipmentStatuses[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        failureCount += 1;
      }
    }
    if (failureCount == 0) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }

  }

  @RequestMapping(value = "statusLookup")
  public String statusLookup(String navTabId, Model model, @ModelAttribute("condition") EquipmentStatusCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<EquipmentStatus> equipmentStatusList = equipmentStatusService.selectEquipmentStatusList(condition);
    int count = equipmentStatusService.selectEquipmentStatusCount(condition);
    condition.setData(equipmentStatusList);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID,  navTabId);
    return "em/equipmentStatus/equipmentStatus_lookup";
  }

  @RequestMapping(value = "statusDropdown")
  @ResponseBody
  public Object statusDropdown(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentStatusCondition condition) {

    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    return equipmentStatusService.selectEquipmentStatusForSuggest(condition);
  }

}
